package davelha.common;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

//public class Game extends UnicastRemoteObject implements BaseGameInterface{
public class Game extends UnicastRemoteObject implements BaseGameInterface{

	/**
	 * 
	 */
	private static final long	serialVersionUID	= -2863277857558190082L;
	public static final char	gameSymbol[]		= { '0', 'X' };

	private Player				player1				= null;
	private Player				player2				= null;
	private int					gameState			= -1;
	private String				gameName			= null;
	private int					gameId				= -1;
	private char				gameTab[]			= null;
	
	public Game(String nameOfGame) throws RemoteException{
		gameState = STATE_BEGINING; //seta o estado inicial do jogo
		gameName = nameOfGame;
		gameTab = new char[9];
		gameTab[0] = 'l';
		gameTab[1] = 'l';
		gameTab[2] = 'l';
		gameTab[3] = 'l';
		gameTab[4] = 'l';
		gameTab[5] = 'l';
		gameTab[6] = 'l';
		gameTab[7] = 'l';
		gameTab[8] = 'l';
		
	}
	
	public Player getPlayer1() throws RemoteException{
		return player1;
	}
	
	public void setPlayer1(Player player1) {
		this.player1 = player1;
	}
	
	public Player getPlayer2() throws RemoteException{
		return player2;
	}
	
	public void setPlayer2(Player player2) {
		this.player2 = player2;
	}
	
	public int getGameState() throws RemoteException{
		return gameState;
	}
	
	public void setGameState(int gameState) {
		this.gameState = gameState;
	}
	
	public String getGameName() throws RemoteException{
		return gameName;
	}
	
	public void setGameName(String gameName) throws RemoteException{
		this.gameName = gameName;
	}

	public int getGameId() throws RemoteException{
		return gameId;
	}

	public void setGameId(int gameId) {
		this.gameId = gameId;
	}
	
	/**
	 * Sobreescreve o to string para retornar as informações do jogo
	 */
	@Override
	public String toString(){
		StringBuffer resultado = new StringBuffer();
		
		resultado.append("Nome do jogo: ");
		try {
			resultado.append(this.getGameName() + "\n");
		} catch (RemoteException e) {
			System.out.println("Remote exception no toStrig da classe game: " + e.getMessage());
		}
		return resultado.toString();
	}
	
	public boolean setChar(int pos, char input) throws RemoteException{
		if(gameTab[pos - 1] != 'l'){
			return false;
		}else{
			gameTab[pos-1] = input;
		}
		
		return true;
	}
	
	public char[] getGameTab() throws RemoteException{
		return this.gameTab;
	}
	
	public boolean gameOver() throws RemoteException{
		//Combinações possiveis de vitoria
		
		//Player 1 = 'X'
		char p1 = 'X';
		char p2 = '0';
		
		//1 - linhas horizontais
		if((gameTab[0] == p1 && gameTab[1] == p1 && gameTab[2] == p1)
				|| (gameTab[3] == p1 && gameTab[4] == p1 && gameTab[5] == p1)
				|| (gameTab[6] == p1 && gameTab[7] == p1 && gameTab[8] == p1)
				){
			//System.out.println("Player 1 ganhou!!!");
			return true;
		}
		//2 - linhas verticais
		else if((gameTab[0] == p1 && gameTab[3] == p1 && gameTab[6] == p1) 
				|| (gameTab[1] == p1 && gameTab[4] == p1 && gameTab[7] == p1)
				|| (gameTab[2] == p1 && gameTab[5] == p1 && gameTab[8] == p1)){
			return true;
		}
		//3 - Diagonais
		else if((gameTab[0] == p1 && gameTab[4] == p1 && gameTab[8] == p1)
				|| (gameTab[2] == p1 && gameTab[4] == p1 && gameTab[6] == p1)
				){
			return true;
		}
		//p2
		//1 - linhas horizontais
		else if((gameTab[0] == p2 && gameTab[1] == p2 && gameTab[2] == p2)
				|| (gameTab[3] == p2 && gameTab[4] == p2 && gameTab[5] == p2)
				|| (gameTab[6] == p2 && gameTab[7] == p2 && gameTab[8] == p2)
				){
			//System.out.println("Player 1 ganhou!!!");
			return true;
		}
		//2 - linhas verticais
		else if((gameTab[0] == p2 && gameTab[3] == p2 && gameTab[6] == p2) 
				|| (gameTab[1] == p2 && gameTab[4] == p2 && gameTab[7] == p2)
				|| (gameTab[2] == p2 && gameTab[5] == p2 && gameTab[8] == p2)){
			return true;
		}
		//3 - Diagonais
		else if((gameTab[0] == p2 && gameTab[4] == p2 && gameTab[8] == p2)
				|| (gameTab[2] == p2 && gameTab[4] == p2 && gameTab[6] == p2)
				){
			return true;
		}

		return false;
	}
}
